package com.sun.androidexerciseproject.function.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;

import com.sun.androidexerciseproject.function.logreport.util.LogUtil;

import java.lang.ref.WeakReference;

public class MessageService extends Service {
    private final Messenger mMessager = new Messenger(new IncomingHandler(this));

    public MessageService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mMessager.getBinder();
    }

    private void testMethod(int what) {
        LogUtil.d("testMethod what " + what);
    }

    private static class IncomingHandler extends Handler {
        private final WeakReference<MessageService> mReference;

        IncomingHandler(MessageService service) {
            mReference = new WeakReference<>(service);
        }

        @Override
        public void handleMessage(Message msg) {
            MessageService service = mReference.get();
            int what = msg.what;
            LogUtil.d("handleMessage what " + what);
            service.testMethod(what);
            super.handleMessage(msg);
        }
    }

}
